<?php
namespace app\teacher\controller;

use think\Db;

class Student extends Base
{
    public function index()
    {
        if(request()->isAjax()){
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['s_name'] = ['like',"%" . $keyword . "%"];
            }
            //用户性别
            if(isset($sex) && $sex!="")
            {
                $map['sex'] = $sex;
            }
            //用户状态
            if(isset($status) && $status!="")
            {
                $map['status'] = $status;
            }
            try{
                $count = Db::name('user_student')->where($map)->count();  //总条目数据
                $lists = Db::name('user_student')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }else {
            return $this->fetch();
        }
    }

    /**
     * @return \think\response\Json
     * field: "s_name"
     * id: "1"
     * value: "张三1"
     */
    public function change_name()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $ret = $this->_user_student->save([$data['field'] => $data['value']],['id' => $data['id']]);
            if(!$ret) {
                return show('200','未做任何更改','0');
            }
            return show('200','更改成功','1');
        }else{
            return show('500','非法操作','0');
        }
    }

    public function class_info_alert()
    {
        if (request()->isAjax()) {
            $data = input('post.');
//            $ret = $this->_user_student->save([$data['field'] => $data['value']],['id' => $data['id']]);
//            if(!$ret) {
//                return show('200','未做任何更改','0');
//            }
//            return show('200','更改成功','1');
            return show('200','更改成功',$data);
        }else{
            return show('500','非法操作','0');
        }
    }
}